package com.guojiaming.shop.exception;

import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * @author vivo
 */
public class ErrorInfo {
    private List<FieldError> error;

    public ErrorInfo(BindException exception) {
        error = exception.getFieldErrors();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < error.size(); i++) {
            builder.append("字段")
                    .append(error.get(i).getField())
                    .append("赋值错误,原因:")
                    .append(error.get(i).getDefaultMessage());
            if (i < error.size() - 1) {
                builder.append(",");
            }
        }
        return builder.toString();
    }
}
